<?php
/*
 * Created on 01.02.2006
 *
 * Extra functions for use in EL
 */

m2import('phpinchains/util/RangeGenerator.php');
m2import('phpinchains/util/ConvertHelper.php');


/**
 * Formats a file size in a human readable format
 *
 * @param int $filesize
 * @return string
 */
function formatFilesize($filesize){

    if(is_numeric($filesize)){
    $decr = 1024; $step = 0;
    $prefix = array('Byte','KB','MB','GB','TB','PB');

    while(($filesize / $decr) > 0.9){
        $filesize = $filesize / $decr;
        $step++;
    }
    return round($filesize,2).' '.$prefix[$step];
    } else {

    return 'NaN';
    }

}

function toGermanDate ($date, $skipYear = false) {
    return ConvertHelper::toGermanDate($date, $skipYear);
}

/**
 * Vergleicht die Jahre von zwei Daten
 *
 * @param <type> $date1
 * @param <type> $date2
 * @return <type>
 */
function sameYear ($date1, $date2) {
    if (preg_match('/(\d{4})\-\d{1,2}\-\d{1,2}/', $date1, $matches1)) {
        if (preg_match('/(\d{4})\-\d{1,2}\-\d{1,2}/', $date2, $matches2)) {
            return ($matches1[1] == $matches2[1]);
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function cutSeconds ($time) {
    if (preg_match('/(\d\d:\d\d):\d\d/', $time, $matches)) {
        return $matches[1];
    }
    return $time;
}

/**
 * alternative selection of a and b (and c)
 */
function alt ($a, $b, $c = null)
{
    $retval = (strlen($a))?$a:$b;
    return ($retval)?$retval:$c;
}
/**
 * Create assoz array
 * 
 * Now replaced by new syntax:
 * [] - empty array
 * [val1, val2, ...] - indexed array
 * [key1 => val1, key2 => val2, ...] - assoziative array
 * 
 * @param type $array
 * @return type 
 */
function aa($array) 
{
	$narr = array();
	for ($i = 0; $i < count($array); $i+=2) {
		$narr[$array[$i]] = $array[$i+1];
	}
	return $narr;
}

function __unpack_val_arr(& $arr, $el2)
{
    if (is_array($el2))
    {
        $hld = $el2[0];
        array_shift($el2);
        if (count($el2)>0)
            array_unshift($arr,$el2);
        return $hld; 
    }
    return $el2;
}

/**
 * Runtime function for executing translated EL code
 * used for execution of arrow expressions like a->b[5]->v in a php4 and php5 
 * compatible way
 * 
 * <id> -> $actionform->get
 * actionform -> $actionform->getArray
 * globalScope -> $env->getGlobal
 * requestScope -> $env->getParam
 * reqScp -> $env->getParam
 * serverScope -> $env->getServer
 * cookieScope -> $env->getCookie
 * fileScope -> $env->getFile
 * envScope -> $env->getEnv
 * sessionScope -> $env->getSession
 * responseScope -> $env->getAttribute
 * respScp -> $env->getAttribute
 * configScope -> $env->getConfig
 */
function __id (& $env, phpinchains_ActionForm & $actionform, $arr)
{
    $arr_hold = $arr;
    $el = array_shift($arr);
    $val = '';
    if (count($arr)==0)
    {
        if ($el == 'actionform')
            return $actionform->getArray();

        return $actionform->get($el);
    }
    $el2 = array_shift($arr);
    switch ($el)
    {
        case 'globalScope':
            $val = $env->getGlobal($el2);
            break;
        case 'reqScp':
        case 'requestScope':
            $val = $env->getParam(__unpack_val_arr($arr, $el2));
            break;
        case 'serverScope':
            $val = $env->getServer(__unpack_val_arr($arr, $el2));
            break;
        case 'cookieScope':
            $val = $env->getCookie(__unpack_val_arr($arr, $el2));
            break;
        case 'fileScope':
            $val = $env->getFile(__unpack_val_arr($arr, $el2));
            break;
        case 'envScope':
            $val = $env->getEnv(__unpack_val_arr($arr, $el2));
            break;
        case 'configScope':
            $val = $env->getConfig(__unpack_val_arr($arr, $el2));
            break;
        case 'sessionScope':
            $val = $env->getSession(__unpack_val_arr($arr, $el2));
            break;
        case 'respScp':
        case 'responseScope':
            $val = $env->getAttribute(__unpack_val_arr($arr, $el2));
            break;
        case 'actionform':
            $val = $actionform->getArray();
            break;
        default:
            $val = $actionform->get($el);
            array_unshift($arr, $el2);
            break;
    }
    while (count($arr)>0)
    {
        $el = array_shift($arr);
        if (is_array($el))
            for ($i = 0; $i < count($el); $i++)
            {
                $val = $val[$el[$i]];
            }
        else
        {
            if (is_object($val))
            {
                /*if (method_exists($val, $el))
                {
                    $val = $val->$el();
                }
                elseif (method_exists($val, 'get'))
                {*/
                    $val = $val->get($el);
                //}
            }
            elseif (isset($val[$el]))
                $val = $val[$el];
            else
                $val = null;
        }
    }
    return $val;
}

function __arrayaccess ($val, $arr) {
    while (count($arr)>0)
    {
        $el = array_shift($arr);
        if (is_array($el))
            for ($i = 0; $i < count($el); $i++)
            {
                $val = $val[$el[$i]];
            }
        else
        {
            if (is_object($val))
            {
                /*if (method_exists($val, $el))
                {
                    $val = $val->$el();
                }
                elseif (method_exists($val, 'get'))
                {*/
                    $val = $val->get($el);
                //}
            }
            elseif (isset($val[$el]))
                $val = $val[$el];
            else
                $val = null;
        }
    }
    return $val;
}
function optionNumberValues ($start, $end, $length = 0) {
	$range = array();
	if ($length) {
	 	$range = RangeGenerator::rangeLZ($start, $end, $length);
	} else {
        $range = RangeGenerator::range($start, $end);
	}
	$res = array();
	for ($i = 0; $i < count($range); $i++) {
		$res[] = array('value' => $range[$i]);
	}
	return $res;
}

function cast ($value, $type) {
    switch ($type) {
        case 'int':
            return (int) $value;
        case 'bool':
            return (boolean) $value;
        case 'float':
            return (float) $value;
        case 'string':
            return (string) $value;
    }
}

?>